import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yres/tool/constant.dart';
import 'package:flutter_yres/tool/AppSaveInfoUtil.dart';
import 'package:flutter_yres/tool/BlankToolBarTool.dart';
import 'package:dio/dio.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import '../../tool/md5_sign.dart';
import 'dart:async';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../../tool/Toast.dart';

class BanlanceChangePage extends StatefulWidget {
  BanlanceChangePage({Key key}) : super(key: key);

  @override
  _BanlanceChangePageState createState() => _BanlanceChangePageState();
}

class _BanlanceChangePageState extends State<BanlanceChangePage> {
  BlankToolBarModel barModel = BlankToolBarModel();
  ScrollController scrollController = ScrollController();
  String account;
  String account2;
  String money;
  String money2;
  String pwd;
  String token;
  String code;

  String codeString = "获取验证码";
  int codeStatus = 1;
  Timer _countdownTimer;
  int _countdownNum = 59;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
          title: Text(
            '余额互转',
            style: TextStyle(
                color: black,
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
          ),
          backgroundColor: white,
          brightness: Brightness.light,
          elevation: 0.2,
          iconTheme: IconThemeData(color: black_99),
        ),
        body: BlankToolBarTool.blankToolBarWidget(context,
            model: barModel,
            body: SingleChildScrollView(
                controller: scrollController,
                child: Container(
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                      Container(
                        margin: EdgeInsets.all(
                            ScreenUtil.getInstance().setWidth(15)),
                        alignment: Alignment.center,
                        child: Text(
                          '可用余额',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(BIG_FONT_SIZE)),
                        ),
                      ),
                      new Container(
                        margin: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setWidth(15),
                            0.0,
                            ScreenUtil.getInstance().setWidth(15),
                            ScreenUtil.getInstance().setWidth(15)),
                        decoration: new BoxDecoration(
                          border: new Border.all(
                              color: Colors.grey, width: 0.5), // 边色与边宽度
                          color: Colors.white, // 底色
                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                          borderRadius:
                              new BorderRadius.circular((5.0)), // 也可控件一边圆角大小
                        ),
                        padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                        child: new Row(
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: Text(
                                '账号:',
                                style: TextStyle(
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(NORMAL_FONT_SIZE)),
                              ),
                            ),
                            new Expanded(
                                flex: 3,
                                child: new TextField(
                                  maxLines: 1,
                                  cursorColor: mainColor,
                                  onChanged: (value) {
                                    account = value;
                                  },
                                  keyboardAppearance: Brightness.light,
                                  // controller: TextEditingController.fromValue(
                                  //     TextEditingValue(
                                  //         text: account == null ? "" : account,
                                  //         selection: TextSelection.fromPosition(
                                  //             TextPosition(
                                  //                 affinity:
                                  //                     TextAffinity.downstream,
                                  //                 offset: account == null
                                  //                     ? 0
                                  //                     : account.length)))),
                                  style: TextStyle(
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(NORMAL_FONT_SIZE)),
                                  decoration: new InputDecoration(
                                      hintText: "",
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(NORMAL_FONT_SIZE))),
                                )),
                          ],
                        ),
                      ),
                      new Container(
                        margin: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setWidth(15),
                            0.0,
                            ScreenUtil.getInstance().setWidth(15),
                            ScreenUtil.getInstance().setWidth(15)),
                        decoration: new BoxDecoration(
                          border: new Border.all(
                              color: Colors.grey, width: 0.5), // 边色与边宽度
                          color: Colors.white, // 底色
                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                          borderRadius:
                              new BorderRadius.circular((5.0)), // 也可控件一边圆角大小
                        ),
                        padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                        child: new Row(
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: Text(
                                '确认账号:',
                                style: TextStyle(
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(NORMAL_FONT_SIZE)),
                              ),
                            ),
                            new Expanded(
                                flex: 3,
                                child: new TextField(
                                  maxLines: 1,
                                  cursorColor: mainColor,
                                  onChanged: (value) {
                                    account2 = value;
                                  },
                                  keyboardAppearance: Brightness.light,
                                  // controller: TextEditingController.fromValue(
                                  //     TextEditingValue(
                                  //         text: account2 == null ? "" : account2,
                                  //         selection: TextSelection.fromPosition(
                                  //             TextPosition(
                                  //                 affinity:
                                  //                     TextAffinity.downstream,
                                  //                 offset: account2 == null
                                  //                     ? 0
                                  //                     : account2.length)))),
                                  style: TextStyle(
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(NORMAL_FONT_SIZE)),
                                  decoration: new InputDecoration(
                                      hintText: "",
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(NORMAL_FONT_SIZE))),
                                )),
                          ],
                        ),
                      ),
                      new Container(
                        margin: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setWidth(15),
                            0.0,
                            ScreenUtil.getInstance().setWidth(15),
                            ScreenUtil.getInstance().setWidth(15)),
                        decoration: new BoxDecoration(
                          border: new Border.all(
                              color: Colors.grey, width: 0.5), // 边色与边宽度
                          color: Colors.white, // 底色
                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                          borderRadius:
                              new BorderRadius.circular((5.0)), // 也可控件一边圆角大小
                        ),
                        padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                        child: new Row(
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: Text(
                                '金额:',
                                style: TextStyle(
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(NORMAL_FONT_SIZE)),
                              ),
                            ),
                            new Expanded(
                                flex: 3,
                                child: new TextField(
                                  maxLines: 1,
                                  cursorColor: mainColor,
                                  onChanged: (value) {
                                    money = value;
                                  },
                                  keyboardAppearance: Brightness.light,
                                  // controller: TextEditingController.fromValue(
                                  //     TextEditingValue(
                                  //         text: money == null ? "" : money,
                                  //         selection: TextSelection.fromPosition(
                                  //             TextPosition(
                                  //                 affinity:
                                  //                     TextAffinity.downstream,
                                  //                 offset: money == null
                                  //                     ? 0
                                  //                     : money.length)))),
                                  style: TextStyle(
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(NORMAL_FONT_SIZE)),
                                  decoration: new InputDecoration(
                                      hintText: "",
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(NORMAL_FONT_SIZE))),
                                )),
                          ],
                        ),
                      ),
                      new Container(
                        margin: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setWidth(15),
                            0.0,
                            ScreenUtil.getInstance().setWidth(15),
                            ScreenUtil.getInstance().setWidth(15)),
                        decoration: new BoxDecoration(
                          border: new Border.all(
                              color: Colors.grey, width: 0.5), // 边色与边宽度
                          color: Colors.white, // 底色
                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                          borderRadius:
                              new BorderRadius.circular((5.0)), // 也可控件一边圆角大小
                        ),
                        padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                        child: new Row(
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: Text(
                                '确认金额:',
                                style: TextStyle(
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(NORMAL_FONT_SIZE)),
                              ),
                            ),
                            new Expanded(
                                flex: 3,
                                child: new TextField(
                                  maxLines: 1,
                                  cursorColor: mainColor,
                                  onChanged: (value) {
                                    money2 = value;
                                  },
                                  keyboardAppearance: Brightness.light,
                                  // controller: TextEditingController.fromValue(
                                  //     TextEditingValue(
                                  //         text: money2 == null ? "" : money2,
                                  //         selection: TextSelection.fromPosition(
                                  //             TextPosition(
                                  //                 affinity:
                                  //                     TextAffinity.downstream,
                                  //                 offset: money2 == null
                                  //                     ? 0
                                  //                     : money2.length)))),
                                  style: TextStyle(
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(NORMAL_FONT_SIZE)),
                                  decoration: new InputDecoration(
                                      hintText: "",
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(NORMAL_FONT_SIZE))),
                                )),
                          ],
                        ),
                      ),
                      new Container(
                        margin: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setWidth(15),
                            0.0,
                            ScreenUtil.getInstance().setWidth(15),
                            ScreenUtil.getInstance().setWidth(15)),
                        decoration: new BoxDecoration(
                          border: new Border.all(
                              color: Colors.grey, width: 0.5), // 边色与边宽度
                          color: Colors.white, // 底色
                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                          borderRadius:
                              new BorderRadius.circular((5.0)), // 也可控件一边圆角大小
                        ),
                        padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                        child: new Row(
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: Text(
                                '支付密码:',
                                style: TextStyle(
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(NORMAL_FONT_SIZE)),
                              ),
                            ),
                            new Expanded(
                                flex: 3,
                                child: new TextField(
                                  obscureText: true,
                                  maxLines: 1,
                                  cursorColor: mainColor,
                                  onChanged: (value) {
                                    pwd = value;
                                  },
                                  keyboardAppearance: Brightness.light,
                                  // controller: TextEditingController.fromValue(
                                  //     TextEditingValue(
                                  //         text: pwd == null ? "" : pwd,
                                  //         selection: TextSelection.fromPosition(
                                  //             TextPosition(
                                  //                 affinity:
                                  //                     TextAffinity.downstream,
                                  //                 offset: pwd == null
                                  //                     ? 0
                                  //                     : pwd.length)))),
                                  style: TextStyle(
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(NORMAL_FONT_SIZE)),
                                  decoration: new InputDecoration(
                                      hintText: "",
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(NORMAL_FONT_SIZE))),
                                )),
                          ],
                        ),
                      ),
                      new Container(
                        margin: EdgeInsets.fromLTRB(
                            ScreenUtil.getInstance().setWidth(15),
                            0.0,
                            ScreenUtil.getInstance().setWidth(15),
                            ScreenUtil.getInstance().setWidth(15)),
                        decoration: new BoxDecoration(
                          border: new Border.all(
                              color: Colors.grey, width: 0.5), // 边色与边宽度
                          color: Colors.white, // 底色
                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                          borderRadius:
                              new BorderRadius.circular((5.0)), // 也可控件一边圆角大小
                        ),
                        padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                        child: new Row(
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: Text(
                                '短信验证:',
                                style: TextStyle(
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(NORMAL_FONT_SIZE)),
                              ),
                            ),
                            new Expanded(
                                flex: 2,
                                child: new TextField(
                                  keyboardType: TextInputType.number,
                                  maxLines: 1,
                                  cursorColor: mainColor,
                                  onChanged: (value) {
                                    code = value;
                                  },
                                  keyboardAppearance: Brightness.light,
                                  // controller: TextEditingController.fromValue(
                                  //     TextEditingValue(
                                  //         text: code == null ? "" : code,
                                  //         selection: TextSelection.fromPosition(
                                  //             TextPosition(
                                  //                 affinity:
                                  //                     TextAffinity.downstream,
                                  //                 offset: code == null
                                  //                     ? 0
                                  //                     : code.length)))),
                                  style: TextStyle(
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(NORMAL_FONT_SIZE)),
                                  decoration: new InputDecoration(
                                      hintText: "",
                                      border: InputBorder.none,
                                      hintStyle: TextStyle(
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(NORMAL_FONT_SIZE))),
                                )),
                            Container(
                              width: ScreenUtil.getInstance().setWidth(200),
                              child: new MaterialButton(
                                // color: codeStatus == 1 ? Colors.blue : Colors.grey,
                                textColor:
                                    codeStatus == 1 ? Colors.blue : Colors.grey,
                                child: new Text(
                                  codeString,
                                  style: TextStyle(
                                      fontSize:
                                          ScreenUtil.getInstance().setSp(26)),
                                ),
                                onPressed: () {
                                  sendNewCode();
                                },
                              ),
                            )
                          ],
                        ),
                      ),
                      new Container(
                        width: double.infinity,
                        margin: EdgeInsets.all(
                            ScreenUtil.getInstance().setHeight(DP_40)),
                        height: ScreenUtil.getInstance().setWidth(DP_90),
                        decoration: new BoxDecoration(
                            color: Colors.red,
                            borderRadius: BorderRadius.circular(DP_80)),
                        padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                        child: new FlatButton(
                          child: new Padding(
                            padding: EdgeInsets.all(0.0),
                            child: new Text(
                              "确定",
                              style: new TextStyle(
                                  color: Colors.white,
                                  fontSize: ScreenUtil.getInstance()
                                      .setSp(NORMAL_FONT_SIZE)),
                            ),
                          ),
                          onPressed: () {
                            balance();
                          },
                        ),
                      )
                    ])))));
  }

  void initState() {
    // TODO: implement initState
    super.initState();
    barModel.outSideCallback = focusNodeChange;
    getString(TOKEN).then((value) {
      token = value;
    });
  }

  void focusNodeChange() {
    setState(() {});
  }

  @override
  void dispose() {
    barModel.removeFocusListeners();
    // AMapLocationClient.shutdown();
    super.dispose();
  }

  sendNewCode() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    //  if (account == '') {
    //   Toast.toast(context, msg: '填写绑定手机号');
    //   return;
    // }
    Map map = Map();
    map["phone"] = preferences.get("name");
    map['sign_type'] = 'yres.sms';
    Response<String> response = await HttpUtil()
        .post(Api.BaseUrl + '/app.php?c=Sms&a=sendPay', data: getSign(map));
    Map userMap = json.decode(response.data);
    Toast.toast(context, msg: userMap['msg']);
    int code = userMap['code'];
    if (code == 1) {
      print(userMap);
      setState(() {
        codeStatus = 0;
        if (_countdownTimer != null) {
          return;
        }
        codeString = '${_countdownNum--}重新获取';
        _countdownTimer = new Timer.periodic(new Duration(seconds: 1), (timer) {
          setState(() {
            if (_countdownNum > 0) {
              codeStatus = 0;
              codeString = '${_countdownNum--}重新获取';
            } else {
              codeString = '获取验证码';
              codeStatus = 1;
              _countdownNum = 59;
              _countdownTimer.cancel();
              _countdownTimer = null;
            }
          });
        });
      });
    }
  }

  balance() async {
    if(account==''||account==null){
      Toast.toast(context,msg:'请填写完整信息');
      return;
    }
    if(account2==''||account2==null){
      Toast.toast(context,msg:'请填写完整信息');
      return;
    }
     if(money==''||money==null){
      Toast.toast(context,msg:'请填写完整信息');
      return;
    }
    if(money2==''||money2==null){
      Toast.toast(context,msg:'请填写完整信息');
      return;
    }
    if(pwd==''||pwd==null){
      Toast.toast(context,msg:'请填写完整信息');
      return;
    }
    if(code==''||code==null){
      Toast.toast(context,msg:'请填写完整信息');
      return;
    }
// /app.php?c=UserBalance&a=transfer
    Map map = Map();
    map["token"] = token;
    map['sign_type'] = 'cdd.UserBalance.transfer';
    map["account"] = account; //varchar(50)	是	收款方账号
    map["repeat_account"] = account2; //	varchar(50)	是	确认收款方账号
    map["money"] = money; //	float(12,2)	是	转账金额
    map["repeat_money"] = money2; //		float(12,2)	是	确认转账金额
    map["draw_pwd"] = pwd; //	varchar(20)	是	提现密码
// map["remark"] = 	//	varchar(200)	否	备注
    map["code"] = code; //	varchar(6)	是	短信验证码

    Response<String> response = await HttpUtil().post(
        Api.BaseUrl + '/app.php?c=UserBalance&a=transfer',
        data: getSign(map));
        Map userMap = json.decode(response.data);
    Toast.toast(context, msg: userMap['msg']);
    // int code = userMap['code'];
    print(userMap);
    if (userMap['code'] == 1) {
       Navigator.of(context).pop();
    }
  }
}
